package com.healthyeating.manage.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.manage.domain.Share;
import com.healthyeating.manage.mapper.ShareMapper;
import com.healthyeating.manage.service.IMyShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 我的分享Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-03-27
 */
@Service
public class IMyShareServiceImpl implements IMyShareService {

    @Autowired
    private ShareMapper shareMapper;

    /**
     * 查询发现分享
     *
     * @param shareId 发现分享主键
     * @return 发现分享
     */
    @Override
    public Share selectShareByShareId(Long shareId) {
        Share share = shareMapper.selectShareByShareId(shareId);
        if (share != null) {
            if (share.getShareFile() != null && !share.getShareFile().isEmpty()) {
                // 将 JSON 字符串解析为 JSONArray
                JSONArray fileArray = JSON.parseArray(share.getShareFile());
                // 将 JSONArray 转换为逗号分隔的字符串
                String fileString = fileArray.stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(","));
                // 设置转换后的字符串回 Share 对象
                share.setShareFile(fileString);
            }
        }
        return share;
    }

    /**
     * 查询发现分享列表
     *
     * @param share 发现分享
     * @return 发现分享
     */
    @Override
    public List<Share> selectShareList(Share share) {
        // 查询分享列表
        List<Share> shareList = shareMapper.selectShareList(share);
        // 将数组形式的文件地址转换成逗号分隔的字符串
        return shareList.stream().map(_share -> {
            String shareFile = _share.getShareFile();
            if (shareFile != null && !shareFile.isEmpty()) {
                // 将 JSON 字符串解析为 JSONArray
                JSONArray fileArray = JSON.parseArray(shareFile);
                // 将 JSONArray 转换为逗号分隔的字符串
                String fileString = fileArray.stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(","));
                // 设置转换后的字符串回 Share 对象
                _share.setShareFile(fileString);
            }
            return _share;
        }).collect(Collectors.toList());
    }

    /**
     * 新增发现分享
     *
     * @param share 发现分享
     * @return 结果
     */
    @Override
    public int insertShare(Share share) {
        share.setCreateTime(DateUtils.getNowDate());
        return shareMapper.insertShare(share);
    }

    /**
     * 修改发现分享
     *
     * @param share 发现分享
     * @return 结果
     */
    @Override
    public int updateShare(Share share) {
        share.setUpdateTime(DateUtils.getNowDate());
        return shareMapper.updateShare(share);
    }

    /**
     * 批量删除发现分享
     *
     * @param shareIds 需要删除的发现分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareIds(Long[] shareIds) {
        return shareMapper.deleteShareByShareIds(shareIds);
    }

    /**
     * 删除发现分享信息
     *
     * @param shareId 发现分享主键
     * @return 结果
     */
    @Override
    public int deleteShareByShareId(Long shareId) {
        return shareMapper.deleteShareByShareId(shareId);
    }

}
